//
//  GYInfoViewController.m
//  research
//
//  Created by 章鱼 on 17/3/29.
//  Copyright © 2017年 simon. All rights reserved.
//

#import "GYInfoViewController.h"
#import "GYUserInfoViewController.h"
#import "GYNewsInfoModel.h"
#import "GYQuestInfoModel.h"
#import "XFWkwebView.h"
#import "GYLoginViewController.h"

@interface GYInfoViewController ()

@property (nonatomic, strong) NSString *infotitle;
@property (nonatomic, assign) GYHomeInfoStytleType type;
@property (nonatomic, assign) NSInteger  newsId;
@property (nonatomic, strong) UIImageView *pictureView;
@property (nonatomic, strong) UILabel *titleLable;
@property (nonatomic, strong) UIView *firstLineView;
@property (nonatomic, strong) UIView *secondLineView;
@property (nonatomic, strong) UILabel *infoLable;
@property (nonatomic, strong) UIImageView *likeView;
@property (nonatomic, strong) UILabel *likeNumber;
@property (nonatomic, strong) UIImageView *readView;
@property (nonatomic, strong) UILabel * readNumber;
@property (nonatomic, strong) UIView *questionNumberView;
@property (nonatomic, strong) UIView *statusView;
@property (nonatomic, strong) UIButton *answerButton;
@property (nonatomic, strong) UIScrollView *infoScrollView;
@property (nonatomic, strong) UIView *leftView;
@property (nonatomic, strong) UILabel *leftTitle;
@property (nonatomic, strong) UILabel *leftNumber;

@property (nonatomic, strong) UIView *rightView;
@property (nonatomic, strong) UILabel *rightTitle;
@property (nonatomic, strong) UILabel *rightNumber;
@property (nonatomic, strong) NSString * urlstring;
@property (nonatomic, strong) NSString * quetionId;

@end

@implementation GYInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self basicSetup];
    [self intiInfoView];
}

- (instancetype)initWithTitle:(NSString *)title videoType:(GYHomeInfoStytleType )Type statuId:(NSInteger)newsId   {

    self = [super init];
    if (self) {
        _infotitle = title;
        _type = Type;
        _newsId = newsId;
    }
    return self;

}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self addBackButtonItemWithTitle:@"返回" backImg:@"navigation-bar_back_normal@2x"];
    [self addRightButtonItemWithTitle:@"" backImg:@"navigation bar_-details_normal"];
    [self getNewsInfoOrQuetionInfo];
}

- (void)getNewsInfoOrQuetionInfo {

    [[YGHttpManager defaultManager] fetchGetQuestInfoWithQuestionId:_newsId success:^(id responseObj) {
        GYCommonModel *model = [GYCommonModel mj_objectWithKeyValues:responseObj];
        _questModel = [GYQuestInfoModel mj_objectWithKeyValues:model.data];
        [_pictureView sd_setImageWithURL:[NSURL URLWithString:_questModel.picture] placeholderImage:nil];
        _titleLable.text = _questModel.title;
        _infoLable.text =_questModel.content;
        _readNumber.text =[NSString stringWithFormat:@"%ld", (long)_questModel.readCount];
        _likeNumber.text = [NSString stringWithFormat:@"%ld", (long)_questModel.sort];
        _leftNumber.text = _questModel.questionCount;
        _urlstring = _questModel.link;
        _quetionId = [NSString stringWithFormat:@"%ld",(long)_questModel.id];
        
    } failure:^(NSError *error) {
        
    }];

}

- (void)rightButtonItemClick {
    [super rightButtonItemClick];
    GYUserInfoViewController *userCtrl = [[GYUserInfoViewController alloc] init];
    userCtrl.hidesBottomBarWhenPushed = YES;
    userCtrl.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:userCtrl animated:YES];
}


- (void)basicSetup {
    [self initNavBarWithTitle:_infotitle tintColor:[UIColor orz_mainColor]];
    [self.navBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                         [UIColor whiteColor],NSForegroundColorAttributeName,
                                         [UIFont boldSystemFontOfSize:20],
                                         NSFontAttributeName,
                                         nil]];
    [self.view setBackgroundColor:[UIColor orz_globalBackground]];

}
- (void)intiInfoView {
    _infoScrollView = [[UIScrollView alloc] init];
    [self.view addSubview:_infoScrollView];
    _infoScrollView.showsVerticalScrollIndicator = FALSE;
    _infoScrollView.showsHorizontalScrollIndicator = FALSE;
    _infoScrollView.backgroundColor = [UIColor orz_globalBackground];
    
    _pictureView = [[UIImageView alloc] init];
    [_infoScrollView addSubview:_pictureView];
    
    _titleLable = [[UILabel alloc] init];
    [_infoScrollView addSubview:_titleLable];
    _titleLable.font = [UIFont systemFontOfSize:15];
    _titleLable.textColor = [UIColor colorWithHex:@"323232" alpha:1];
    _titleLable.numberOfLines = 0;
    
    _firstLineView = [[UIView alloc] init];
    [_infoScrollView addSubview:_firstLineView];
    _firstLineView.backgroundColor = [UIColor colorWithHex:@"e1e1e1" alpha:1];
    
    _likeView = [[UIImageView alloc] init];
    [_infoScrollView addSubview:_likeView];
    [_likeView setImage:[UIImage imageNamed:@"home_icon_Collection"]];
    
    _likeNumber = [[UILabel alloc] init];
    _likeNumber.font = [UIFont systemFontOfSize:11];
    _likeNumber.textColor = [UIColor orz_textAssist];
    _likeNumber.text = @"100";
    [_infoScrollView addSubview:_likeNumber];
    
    _readView = [[UIImageView alloc] init];
    [_infoScrollView addSubview:_readView];
    [_readView setImage:[UIImage imageNamed:@"home_icon_browse"]];
    
    _readNumber = [[UILabel alloc] init];
    _readNumber.font = [UIFont systemFontOfSize:11];
    _readNumber.text = @"200";
    _readNumber.textColor = [UIColor orz_textAssist];
    [_infoScrollView addSubview:_readNumber];
    
    _secondLineView = [[UIView alloc] init];
    [_infoScrollView addSubview:_secondLineView];
    _secondLineView.backgroundColor = [UIColor colorWithHex:@"e1e1e1" alpha:1];
    
    _infoLable = [[UILabel alloc] init];
    [_infoScrollView addSubview:_infoLable];
    _infoLable.numberOfLines = 0;
    _infoLable.font = [UIFont systemFontOfSize:13];
    _infoLable.textColor = [UIColor colorWithHex:@"909090" alpha:1];
    
    _answerButton = [[UIButton alloc] init];
    [_infoScrollView addSubview:_answerButton];
    [_answerButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _answerButton.backgroundColor = [UIColor orz_mainColor];
    [_answerButton setTitle:@"点击开始答卷" forState:UIControlStateNormal];
    _answerButton.titleLabel.font = [UIFont systemFontOfSize:14];
    _answerButton.layer.cornerRadius = 4.f;
    _answerButton.layer.masksToBounds = YES;
    [_answerButton addTarget:self action:@selector(answerButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    _leftView = [[UIView alloc] init];
    [_infoScrollView addSubview:_leftView];
    _leftView.layer.cornerRadius = 2.f;
    _leftView.layer.masksToBounds = YES;
    _leftView.backgroundColor = [UIColor colorWithHex:@"ebc533" alpha:1];
    
    _leftTitle = [[UILabel alloc] init];
    [_leftView addSubview:_leftTitle];
    _leftTitle.text = @"题量";
    _leftTitle.textColor = [UIColor colorWithHex:@"8c6f00" alpha:1];
    _leftTitle.font = [UIFont systemFontOfSize:11];
    
    _leftNumber = [[UILabel alloc] init];
    [_leftView addSubview:_leftNumber];
    _leftNumber.text = @"55";
    _leftNumber.textColor = [UIColor colorWithHex:@"906f00" alpha:1];
    _leftNumber.font = [UIFont systemFontOfSize:16];
    
    _rightView = [[UIView alloc] init];
    _rightView.layer.cornerRadius = 2.f;
    _rightView.layer.masksToBounds = YES;
    [_infoScrollView addSubview:_rightView];
    _rightView.backgroundColor = [UIColor colorWithHex:@"ee9ac9" alpha:1];
    
    _rightTitle = [[UILabel alloc] init];
    [_rightView addSubview:_rightTitle];
    _rightTitle.text = @"状态";
    _rightTitle.textColor = [UIColor colorWithHex:@"8c6f00" alpha:1];
    _rightTitle.font = [UIFont systemFontOfSize:11];
    
    _rightNumber = [[UILabel alloc] init];
    [_rightView addSubview:_rightNumber];
    _rightNumber.text = @"运行中";
    _rightNumber.textColor = [UIColor colorWithHex:@"906f00" alpha:1];
    _rightNumber.font = [UIFont systemFontOfSize:16];
    
    if (_type == GYNewsInfoType) {
        _answerButton.hidden = YES;
        _leftView.hidden = YES;
        _rightView.hidden = YES;
        _likeNumber.hidden = YES;
        _likeView.hidden = YES;
    }
    
    [self intiInfoViewUI];
}

- (void)intiInfoViewUI {
    [_infoScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(64.f);
        make.bottom.mas_equalTo(0.f);
        make.left.mas_equalTo(0.f);
        make.right.mas_equalTo(0);
    }];
    
    [_pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0.f);
        make.left.mas_equalTo(0.f);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(155.f*ADJUST_SCREEN_HEIGHT_SCALE);
        
    }];
    
    [_titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12.f);
        make.top.mas_equalTo(_pictureView.mas_bottom).offset(20.f);
        make.right.mas_equalTo(-12.f);

    }];
    
    [_firstLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12.f);
        make.top.mas_equalTo(_titleLable.mas_bottom).offset(10.f);
        make.right.mas_equalTo(-12.f);
        make.height.mas_equalTo(.5f);
    }];
    
    [_readView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_firstLineView.mas_bottom).offset(11.f);
        make.left.mas_equalTo(12.f);
    }];
    [_readNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_readView.mas_centerY);
        make.left.mas_equalTo(_readView.mas_right).offset(4.f);
        
    }];
    [_likeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_readView.mas_centerY);
        make.left.mas_equalTo(_readNumber.mas_right).offset(13.f);
    }];
    
    [_likeNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_readView.mas_centerY);
        make.left.mas_equalTo(_likeView.mas_right).offset(4.f);
        
    }];
    
    [_secondLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12.f);
        make.top.mas_equalTo(_firstLineView.mas_bottom).offset(31.f);
        make.right.mas_equalTo(-12.f);
        make.height.mas_equalTo(.5f);
    }];
    
    [_infoLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_secondLineView.mas_bottom).offset(16.f);
        make.left.mas_equalTo(12.f);
        make.right.mas_equalTo(-12.f);
        make.width.mas_equalTo(SCREEN_WIDTH - 24);

    }];
    
    
    [_leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12.f);
        make.top.mas_equalTo(_infoLable.mas_bottom).offset(20.f);
        make.size.mas_equalTo(CGSizeMake((SCREEN_WIDTH - 25)/2, 44*ADJUST_SCREEN_HEIGHT_SCALE));
    }];
    
    [_leftNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_leftView);
        make.top.mas_equalTo(5.f *ADJUST_SCREEN_HEIGHT_SCALE);
    }];
    
    [_leftTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_leftView);
        make.bottom.mas_equalTo(-5.f*ADJUST_SCREEN_HEIGHT_SCALE);
        
    }];
    
    [_rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-12.f);
        make.top.mas_equalTo(_infoLable.mas_bottom).offset(20.f);
        make.size.mas_equalTo(CGSizeMake((SCREEN_WIDTH - 25)/2, 44*ADJUST_SCREEN_HEIGHT_SCALE));
    }];
    
    [_rightNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_rightView);
        make.top.mas_equalTo(5.f*ADJUST_SCREEN_HEIGHT_SCALE);
    }];
    
    [_rightTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_rightView);
        make.bottom.mas_equalTo(-5.f*ADJUST_SCREEN_HEIGHT_SCALE);
        
    }];
    
    [_answerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-27.f);
        make.left.mas_equalTo(12.f);
        make.right.mas_equalTo(-12.f);
        make.height.mas_equalTo(44.f*ADJUST_SCREEN_HEIGHT_SCALE);
        make.top.mas_equalTo(_leftView.mas_bottom).offset(20.f);
    }];
}
- (void)answerButtonClick:(UIButton *)btn {
    //判断是否登录
    [self userIsLogin];
}

#pragma mark - action 判断是否登录
- (void)userIsLogin {
    
    [[YGHttpManager defaultManager] fetchGetUserIsloginWithtoken:[[GYConfigDBManager defaultManager] gyToken] success:^(id responseObj) {
        //重新登录
        if ([[responseObj objectForKey:@"status"] integerValue] == 0) {
            if ([[GYConfigDBManager defaultManager] userInformation].userName && [[GYConfigDBManager defaultManager] userInformation].password) {
         
                [[YGHttpManager defaultManager] fetchGetUserLoginWithuserName:[[GYConfigDBManager defaultManager] userInformation].userName password:[[GYConfigDBManager defaultManager] userInformation].password
                                                                      success:^(id responseObj) {
                                                                          GYCommonModel *commonmodel = [GYCommonModel mj_objectWithKeyValues:responseObj];
                                                                          if ([[responseObj objectForKey:@"status"] integerValue] == 0) {
                                                                              [VBAlertView showTextHUD:commonmodel.message];
                                                                              return ;
                                                                          }
                                                                          [[GYConfigDBManager defaultManager] setLogin:YES];
                                                                          [[GYConfigDBManager defaultManager] setGYToken:[commonmodel.data objectForKey:@"token"]];
                                                                          GYUserInfoModel *model = [GYUserInfoModel mj_objectWithKeyValues:[commonmodel.data objectForKey:@"user"]];
                                                                          [[GYConfigDBManager defaultManager] setUserInformation:model];
                                                                          [self collectQuetion];
                                                                          
                                                                      } failure:^(NSError *error) {
                                                                          
                                                                      }];

         
            }else {
            
                GYLoginViewController *loginCtrl = [[GYLoginViewController alloc] init];
                UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginCtrl];
                [self presentViewController:nav animated:YES completion:NULL];
            }
            
        }else {
            [self collectQuetion];
        }
    } failure:^(NSError *error) {
        
    }];
}

- (void)collectQuetion {

    [[YGHttpManager defaultManager] fetchGetQuestCollectWithToken:[[GYConfigDBManager defaultManager] gyToken] userId:[NSString stringWithFormat:@"%ld",(long)[[GYConfigDBManager defaultManager] userInformation].id] questionId:_quetionId success:^(id responseObj) {
        if ([[responseObj objectForKey:@"status"] integerValue] == 1) {
        
            XFWkwebView *webView = [[XFWkwebView alloc] init];
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:webView];
            [webView loadWebURLSring:_urlstring];
            [self presentViewController:nav animated:YES completion:nil];
        }
    } failure:^(NSError *error) {
        
    }];

}



@end
